package com.liy.supercloud.ssoserver.aop;

import com.liy.framework.common.annotation.IgnoreUserToken;
import com.liy.framework.common.exception.InvalidRequestException;
import com.liy.framework.common.exception.NotAuthorizationException;
import com.liy.framework.common.extvo.LoginInfo;
import com.liy.framework.common.jwt.JWTHelper;
import com.liy.framework.common.utils.ContextHandler;
import com.liy.framework.common.utils.Tools;
import com.liy.framework.web.spring.utils.WebTools;
import com.liy.supercloud.ssoserver.config.AuthKeyConfig;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;


@Component
public class ServiceAuthorizationInterceptor extends HandlerInterceptorAdapter {

    @Value("${dascloud.authrization.enabled: true}")
    private Boolean authrizationEnabled;

    @Value("${dascloud.authrization.token-header:JwtToken}")
    private String tokenHeader;

    @Value("${dascloud.authrization.auth-key:AuthKey}")
    private String authKeyHeader;

    @Autowired
    private AuthKeyConfig authKeyConfig;


    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String authorization = WebTools.getRequesetParameterOrHeader(request, this.authKeyHeader);
        String jwtToken = WebTools.getRequesetParameterOrHeader(request, this.tokenHeader);
        if (!(handler instanceof HandlerMethod)) {
            throw new InvalidRequestException("无效的请求 [ " + request.getRequestURL().toString() + " ] ");
        } else if (!this.authrizationEnabled) {
            return super.preHandle(request, response, handler);
        } else {
            HandlerMethod handlerMethod = (HandlerMethod)handler;
            IgnoreUserToken annotation = (IgnoreUserToken)handlerMethod.getBeanType().getAnnotation(IgnoreUserToken.class);
            if (annotation == null) {
                annotation = (IgnoreUserToken)handlerMethod.getMethodAnnotation(IgnoreUserToken.class);
            }

            if (annotation != null) {
                return super.preHandle(request, response, handler);
            } else if (Tools.StringIsNullOrSpace(authorization) && Tools.StringIsNullOrSpace(jwtToken)) {
                throw new NotAuthorizationException("未认证的请求.");
            } else {
                try {
                    LoginInfo loginUser = JWTHelper.getInfoFromToken(jwtToken, this.authKeyConfig.getUserPubKey());
                    ContextHandler.setLoginUser(loginUser);
                    ContextHandler.setJwtToken(jwtToken);
                    ContextHandler.setAuthKey(authorization);
                } catch (Exception var9) {
                    throw new NotAuthorizationException("非法的认证请求");
                }

                return super.preHandle(request, response, handler);
            }
        }
    }
}
